<template>
  <div class="cate-container">
    <div class="cate-wrap">
      <div class="cates">
        <div 
          v-for="(cate,index) in cates"
          :key="cate.id"
          @click="switchActive(index)"
          :class="['cate', {
            active: activeIndex === index
          }]">
            {{ cate.name }}
          </div>

      </div>
    </div>
    <div class="item-wrap">
      <div class="items">
        <div class="item" v-for="item in items" :key="item.id" @click="enterDetail(item.id)">
          <img :src="item.pic" alt="" class="item-img">
          <div class="item-info">
            <h5>{{ item.name }}</h5>
            <div class="item-action">
              <span class="price">￥{{ item.minPrice }}</span>
              <van-icon name="gouwuchetianjia" size="18" color="#0ED397" class-prefix="icon"></van-icon>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  <Tabbar />
</template>

<script setup lang="ts" name="CatePage">
import { ref, onMounted } from 'vue'
import Tabbar from '@/components/Tabbar.vue';
import { fetchCates, fetchItems } from '@/api';
import { useRouter } from 'vue-router';
const router = useRouter();

// 点击商品跳转详情页
const enterDetail = (id: number) => {
  router.push({
    path: '/detail',
    query: {
      id
    }
  })
}

const cates = ref<any[]>([]);
const items = ref<any[]>([]);
// 高亮分类下标
const activeIndex = ref<number>(0);
// 点击切换高亮状态
const switchActive = (index: number) => {
  activeIndex.value = index
  getItems()
}


// 请求所有分类
const getCates = () => {
  fetchCates().then(res=> {
    if (res.data.code === 0) {
      cates.value = res.data.data
      getItems();
    }
  })
}

// 请求高亮分类下的商品
const getItems = () => {
  fetchItems({
    categoryId: cates.value[activeIndex.value].id
  }).then(res => {
    if (res.data.code === 0) {
      items.value = res.data.data.result
    }
    if (res.data.code === 700) {
      // 当前分类下没有商品
      items.value = []
    }
    
  })
}

onMounted(() => {
  getCates();
 
})
</script>

<style scoped lang="scss">
.cate-container {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 50px;
  display: flex;

  .cate-wrap {
    width: 3rem;
    height: 100%;
    border-right: 1px solid #eee;
    overflow-y: auto;
    background-color: #f1f1f1;

    .cate {
      font-size: 14px;
      color: #333;
      text-align: center;
      line-height: 36px;
      position: relative;

      &.active {
        background-color: #fff;

        &::after {
          content: '';
          width: 2px;
          height: 36px;
          background-color: #0ED397;
          position: absolute;
          left: 0;
          top: 0;
        }
      }
    }
  }

  .item-wrap {
    width: 7rem;
    height: 100%;
    overflow-y: auto;
    .item{
    display: flex;
    padding: 0 8px;
    margin-top: 5px;
    &-img{
      width: 2rem;
    }
    &-info{
      flex: 1;
      padding-left: 8px;
      
      h5{
        font-size: 14px;
        line-height: 30px;
      }
     .item-action{
      display: flex;
      justify-content: space-between;
      .price {
        font-size: 18px;
        color: #0ED397;
      }
     } 
    }
  }
  }
}
</style>